package org.chenzh.struts.action;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, 
                                                         SessionAware, ServletContextAware,Serializable {

    private static final long      serialVersionUID         = -8578619218734246232L;
    

    protected Logger               logger                   = Logger.getLogger(getClass());

    protected Map<String, String>  model                    = new HashMap<String, String>();
    protected Map<String, Date>    modelDate                = new HashMap<String, Date>();
    protected Map<String, Integer> modelInteger             = new HashMap<String, Integer>();
    
    protected String               message;
    protected String               messageTitle;
    protected String               type;                                                     // json;""(default);
    protected String               actionType;                                               // CRUD
    
    protected ServletContext       context;
    protected HttpServletRequest   request;
    protected HttpServletResponse  response;
    protected Map<String, Object>  session;
    
    private String                 valueListHandler;
    private String                 valueListEntry;
    
    
    
    protected void render(HttpServletResponse response, String text, String contentType) {
        try {
            response.setContentType(contentType);
            response.getWriter().write(text);
        } catch (IOException e) {
        }
    }

    protected void renderText(String text) {
        render(response, text, "text/plain;charset=UTF-8");
    }

    protected void renderHtml(String text) {
        render(response, text, "text/html;charset=UTF-8");
    }

    protected void renderXML(String text) {
        render(response, text, "text/xml;charset=UTF-8");
    }
    
    /**
     * 
     * @param success true,false
     * @param JSON formated data
     * @throws JSONException 
     */
    protected void renderJSON(String success,String data) throws JSONException{
        JSONObject jo = new JSONObject();
        jo.put("success", success);
        jo.put("data", data);
        renderText(jo.toString());
    }
    
    protected void renderDefaulTrue() throws JSONException{
        renderJSON("true","");
    }
    
    protected void  renderDefaultError() throws JSONException{
        renderJSON("false","");
    }
    
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

    public void setServletContext(ServletContext context) {
        this.context = context;
    }

    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    public String getActionType() {
        return actionType;
    }

    public void setActionType(String actionType) {
        this.actionType = actionType;
    }

    public void setDisplayMessage(String key, String message) {
        request.setAttribute(key, message);
    }

    public void setDisplayObject(String key, Object value) {
        request.setAttribute(key, value);
    }

    public String getValueListHandler() {
        return valueListHandler;
    }

    public void setValueListHandler(String valueListHandler) {
        this.valueListHandler = valueListHandler;
    }

    public String getValueListEntry() {
        return valueListEntry;
    }

    public void setValueListEntry(String valueListEntry) {
        this.valueListEntry = valueListEntry;
    }

    public Map<String,String> getModel() {
        return model;
    }

    public void setModel(Map<String,String> model) {
        this.model = model;
    }

    public Map<String, Date> getModelDate() {
        return modelDate;
    }

    public void setModelDate(Map<String, Date> modelDate) {
        this.modelDate = modelDate;
    }

    public Map<String, Integer> getModelInteger() {
        return modelInteger;
    }

    public void setModelInteger(Map<String, Integer> modelInteger) {
        this.modelInteger = modelInteger;
    }


}
